<template>
  <el-dialog width="800px" :visible.sync="dialogVisible" :close-on-click-modal="false" :show-close="false" center>
    <div id="pdfDom">
      <!-- pdfDom -->
      <el-descriptions class="margin-top" title="订单信息" :column="3" border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            收货人
          </template>
          {{ dataForm.addressName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            手机号
          </template>
          {{ dataForm.addressTel }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            地址
          </template>
          {{ dataForm.addressDetail }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-money"></i>
            总金额
          </template>
          {{ `¥${orderAmount}` }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            备注
          </template>
          {{ dataForm.note }}
        </el-descriptions-item>

      </el-descriptions>

      <el-table :data="dataForm.orderItemList" style="width: 100%">
        <el-table-column prop="goodsName" label="商品名" width="180"></el-table-column>
        <el-table-column prop="goodsSpec" label="商品规格" width="180"></el-table-column>
        <el-table-column prop="price" label="单价"></el-table-column>
        <el-table-column prop="count" label="数量"></el-table-column>
        <el-table-column label="金额">
          <template slot-scope="{ row }">
            ￥{{ row.count * row.price }}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- pdfDom -->
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handlePdf()" size="medium">下载打印</el-button>
        <el-button size="medium" @click="handleCancel()">关闭窗口</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import * as OrderAPI from '@/api/order.js';

export default {
  data() {
    return {
      dataForm: {orderItemList: []},
      dialogVisible: false
    };
  },

  computed: {
    //计算一个订单的总金额
    orderAmount() {
      let amount = 0;
      this.dataForm.orderItemList.forEach(item => {
        amount += item.count * item.price;
      });
      return amount.toFixed(2);
    }
  },

  methods: {
    init(id) {
      this.dialogVisible = true;
      this.$nextTick(() => {
        OrderAPI.getDetailById(id).then(result => {
          this.dataForm = result.data; //返回数据结构详见API文档
        });
      });
    },
    handlePdf() {
      this.$message('待完成...'); //TODO
    },
    handleCancel() {
      this.dialogVisible = false;
    }
  }
};
</script>

<style lang="scss" scoped></style>
